package com.example.service.admin;

import com.example.entity.DTO.TeacherDetailDTO;
import com.example.entity.Student;
import com.example.entity.Teacher;
import com.example.entity.Teacher;
import com.example.exception.CustomException;
import com.example.mapper.TeacherMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    public List<TeacherDetailDTO> selectAllTeacher(String teacherName, String deptName) {
        return teacherMapper.selectAllTeacherByNameAndDeptName(teacherName, deptName);
    }

    public void addTeacher(Teacher teacher) {
        String username = teacher.getUsername();
        Teacher dbTeacher = teacherMapper.selectByUsername(username);
        if(dbTeacher != null) {
            throw new CustomException("500", "工号已存在!");
        }
        teacher.setRole("TEA");
        teacherMapper.insert(teacher);
    }

    public Teacher selectTeacher(String username) {
        return teacherMapper.selectByUsername(username);
    }

    public void updateTeacher(Teacher teacher) {
        Integer id = teacher.getId();
        Teacher dbTeacher = teacherMapper.selectById(id);
        if(dbTeacher == null) {
            throw new CustomException("500", "教师ID不存在!");
        }
        String dbUsername = dbTeacher.getUsername();
        String username = teacher.getUsername();
        dbTeacher = teacherMapper.selectByUsername(username);
        if(dbTeacher != null && !dbUsername.equals(username)) {
            throw new CustomException("500", "教师工号已存在!");
        }
        teacher.setRole("TEA");
        teacherMapper.updateById(teacher);
    }

    public void deleteTeacher(Integer id) {
        teacherMapper.deleteById(id);
    }
}
